﻿using UnityEngine;
using UnityEngine.Extension;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Extension;

public class SubjectiveScrollList : ScrollRect
{
    [SerializeField]
    ControlSubject m_subject = new ControlSubject(true);
    public ControlSubject subject { get { return m_subject; } }

    public GameObject TemplateCell;

    protected override void OnEnable()
    {
        base.OnEnable();
        if (subject.hasContent)
            CreateAndInit();
    }

    protected override void Start()
    {
        base.Start();
        if (subject.hasContent)
            CreateAndInit();
    }

    public void CreateAndInit()
    {
        ClearContent();
        if (Application.isPlaying )//&& gameObject.activeSelf)
        {
            IEnumerable items = (IEnumerable)subject.value;
            ToggleGroup toggleGroup = content.GetComponent<ToggleGroup>();
            if (items.isNotNull())
            {
                foreach (var item in items)
                {
                    GameObject cell = Instantiate(TemplateCell) as GameObject;

                    cell.transform.SetParent(content);
                    cell.transform.localScale = Vector3.one;

                    cell.ForEachChildComponent<SubjectiveText>(t => t.subject.SetObject(item));
                    cell.ForEachChildComponent<SubjectiveImage>(t => t.subject.SetObject(item));
                    cell.ForEachChildComponent<SubjectiveRawImage>(t => t.subject.SetObject(item));
                    cell.ForEachChildComponent<SubjectiveLayout>(t => t.subject.SetObject(item));
                    cell.ForEachChildComponent<ActivationBehaviour>(t => t.subject.SetObject(item));
                    cell.ForEachChildComponent<SubjectiveButton>(t => SetUpSubjects(t.subjects, item));
                    cell.ForEachChildComponent<SubjectiveToggle>(t => { SetUpSubjects(t.subjects, item); t.group = toggleGroup; });
                }
                ActivateFirstCell();
            }
        }
    }

    private void ActivateFirstCell()
    {
        ToggleGroup tg = content.GetComponent<ToggleGroup>();
        if (tg)
        {
            Toggle firstToggle = content.GetComponentInChildren<SubjectiveToggle>();
            firstToggle.isOn = true;
        }
    }

    void SetUpSubjects(List<CommandSubject> subjects, object item)
    {
        subjects.ForEach(s =>
        {
            if (s.content.isNone())
                s.content = subject.content;
            s.SetParamObject(item);
        });
    }

    protected override void OnDisable()
    {
        ClearContent();
        base.OnDisable();
    }

    private void ClearContent()
    {
        if (Application.isPlaying)
            content.DestroyChilds();
    }

#if UNITY_EDITOR
    protected override void OnValidate()
    {
        base.OnValidate();
        if (subject.hasUpdate)
            CreateAndInit();
    }
#endif
}
